
import { put, takeEvery } from 'redux-saga/effects'
import paramsCamelCase from "@utils/paramsCamelCase";
import {getBilinInfo} from "@utils/localStorage";
import apiType, {
    GET_WAREHOUSE_LIST,
    GET_WAREHOUSE_INFO,
    GET_WAREHOUSE_EXPRESS_LIST,
    GET_WAREHOUSE_EXPRESS_PLATFORM,
    EDIT_WAREHOUSE,
    CREATE_WAREHOUSE,
    EDIT_WAREHOUSE_EXPRESS_PLATFORM,
    DELETE_WAREHOUSE_EXPRESS_PLATFORM,
    GET_WAREHOUSE_EXPRESS_REGION,
    EDIT_WAREHOUSE_EXPRESS_REGION,
    GET_WAREHOUSE_ORDINARY_EXPRESS_LIST,
    GET_WAREHOUSE_ORDINARY_EXPRESS_REGION,
    EDIT_WAREHOUSE_ORDINARY_EXPRESS_REGION,
    GET_WAREHOUSE_SELECTION,
    GET_ALL_WAREHOUSE_SELECTION,
    SET_SELECTED_WAREHOUSE
} from "@constants/ActionTypes";
import {getAxios, postAxios, putAxios, deleteAxios} from "@utils/api";

// 获取仓库列表
export const getWarehouseList = (params, callback) => ({
    type: apiType(GET_WAREHOUSE_LIST).request,
    params,
    callback
})
// 获取仓库详情
export const getWarehouseInfo = (params, callback) => ({
    type: apiType(GET_WAREHOUSE_INFO).request,
    params,
    callback
})
// 编辑仓库属性
export const editWarehouse = (params, callback) => ({
    type: apiType(EDIT_WAREHOUSE).request,
    params,
    callback
})
// 创建仓库
export const creatWarehouse = (params, callback) => ({
    type: apiType(CREATE_WAREHOUSE).request,
    params,
    callback
})
// 查看同城配送列表
export const getWarehouseExpressList = (params, callback) => ({
    type: apiType(GET_WAREHOUSE_EXPRESS_LIST).request,
    params,
    callback
})
// 查看指定仓库同城配送平台列表
export const getWarehouseExpressPlatformList = (params, callback) => ({
    type: apiType(GET_WAREHOUSE_EXPRESS_PLATFORM).request,
    params,
    callback
})
// 绑定/编辑配送平台信息
export const editWarehouseExpressPlatform = (params, callback) => ({
    type: apiType(EDIT_WAREHOUSE_EXPRESS_PLATFORM).request,
    params,
    callback
})
// 解绑配送平台信息
export const deleteWarehouseExpressPlatform = (params, callback) => ({
    type: apiType(DELETE_WAREHOUSE_EXPRESS_PLATFORM).request,
    params,
    callback
})
// 查看同城配送区域
export const getWarehouseExpressRegion = (params, callback) => ({
    type: apiType(GET_WAREHOUSE_EXPRESS_REGION).request,
    params,
    callback
})
// 配置同城配送区域
export const editWarehouseExpressRegion = (params, callback) => ({
    type: apiType(EDIT_WAREHOUSE_EXPRESS_REGION).request,
    params,
    callback
})
// 查看普通快递列表
export const getWarehouseOrdinaryExpressList = (params, callback) => ({
    type: apiType(GET_WAREHOUSE_ORDINARY_EXPRESS_LIST).request,
    params,
    callback
})
// 查看普通快递配送区域
export const getWarehouseOrdinaryExpressRegion = (params, callback) => ({
    type: apiType(GET_WAREHOUSE_ORDINARY_EXPRESS_REGION).request,
    params,
    callback
})
// 配置普通快递同城配送区域
export const editWarehouseOrdinaryExpressRegion = (params, callback) => ({
    type: apiType(EDIT_WAREHOUSE_ORDINARY_EXPRESS_REGION).request,
    params,
    callback
})
// 仓库列表下拉框
export const getWarehouseSelection = (params, callback) => {
    params.returnAll =  getBilinInfo('storeId') ? false : true
    return{
        type: apiType(GET_WAREHOUSE_SELECTION).request,
        params,
        callback
    }
}
// 所有仓库列表下拉框
export const getAllWarehouseSelection = (params = {}, callback) => {
    params.returnAll = true
    return{
        type: apiType(GET_ALL_WAREHOUSE_SELECTION).request,
        params,
        callback
    }
}
// 设置当前选择的仓库
export const setSelectedWarehouse = (value) => ({
    type: apiType(SET_SELECTED_WAREHOUSE).request,
    params: {
        value
    }
})

const sagas = {
    * [apiType(GET_WAREHOUSE_LIST).request]  ({ params, callback }) {
        try {
            const response = yield getAxios('/inventory/web/warehouse/list', params)
            yield put({ type: apiType(GET_WAREHOUSE_LIST).success, response });
            callback && callback()
        } catch (e) {
            yield put({ type: apiType(GET_WAREHOUSE_LIST).failure});
        }
    },
    * [apiType(GET_WAREHOUSE_INFO).request]  ({ params, callback }) {
        try {
            const response = yield getAxios('/inventory/web/warehouse/info', params)
            yield put({ type: apiType(GET_WAREHOUSE_INFO).success, response });
            callback && callback(paramsCamelCase(response))
        } catch (e) {
            yield put({ type: apiType(GET_WAREHOUSE_INFO).failure});
        }
    },
    * [apiType(EDIT_WAREHOUSE).request]  ({ params, callback }) {
        try {
            const response = yield putAxios('/inventory/web/warehouse/info', params)
            yield put({ type: apiType(EDIT_WAREHOUSE).success, response });
            callback && callback()
        } catch (e) {
            yield put({ type: apiType(EDIT_WAREHOUSE).failure});
        }
    },
    * [apiType(CREATE_WAREHOUSE).request]  ({ params, callback }) {
        try {
            const response = yield postAxios('/inventory/web/warehouse/info', params)
            yield put({ type: apiType(CREATE_WAREHOUSE).success, response });
            callback && callback()
        } catch (e) {
            yield put({ type: apiType(CREATE_WAREHOUSE).failure});
        }
    },
    * [apiType(GET_WAREHOUSE_EXPRESS_LIST).request]  ({ params, callback }) {
        try {
            const response = yield getAxios('/inventory/web/warehouse/city-express/list', params)
            yield put({ type: apiType(GET_WAREHOUSE_EXPRESS_LIST).success, response });
            callback && callback()
        } catch (e) {
            yield put({ type: apiType(GET_WAREHOUSE_EXPRESS_LIST).failure});
        }
    },
    * [apiType(GET_WAREHOUSE_EXPRESS_PLATFORM).request]  ({ params, callback }) {
        try {
            const response = yield getAxios('/inventory/web/warehouse/city-express/platform', params)
            yield put({ type: apiType(GET_WAREHOUSE_EXPRESS_PLATFORM).success, response });
            callback && callback()
        } catch (e) {
            yield put({ type: apiType(GET_WAREHOUSE_EXPRESS_PLATFORM).failure});
        }
    },
    * [apiType(EDIT_WAREHOUSE_EXPRESS_PLATFORM).request]  ({ params, callback }) {
        try {
            const response = yield postAxios('/inventory/web/warehouse/city-express/platform', params)
            yield put({ type: apiType(EDIT_WAREHOUSE_EXPRESS_PLATFORM).success, response });
            callback && callback()
        } catch (e) {
            yield put({ type: apiType(EDIT_WAREHOUSE_EXPRESS_PLATFORM).failure});
        }
    },
    * [apiType(DELETE_WAREHOUSE_EXPRESS_PLATFORM).request]  ({ params, callback }) {
        try {
            const response = yield deleteAxios('/inventory/web/warehouse/city-express/platform', params)
            yield put({ type: apiType(DELETE_WAREHOUSE_EXPRESS_PLATFORM).success, response });
            callback && callback()
        } catch (e) {
            yield put({ type: apiType(DELETE_WAREHOUSE_EXPRESS_PLATFORM).failure});
        }
    },
    * [apiType(GET_WAREHOUSE_EXPRESS_REGION).request]  ({ params, callback }) {
        try {
            const response = yield getAxios('/inventory/web/warehouse/city-express/region', params)
            yield put({ type: apiType(GET_WAREHOUSE_EXPRESS_REGION).success, response });
            callback && callback()
        } catch (e) {
            yield put({ type: apiType(GET_WAREHOUSE_EXPRESS_REGION).failure});
        }
    },
    * [apiType(EDIT_WAREHOUSE_EXPRESS_REGION).request]  ({ params, callback }) {
        try {
            const response = yield postAxios('/inventory/web/warehouse/city-express/region', params)
            yield put({ type: apiType(EDIT_WAREHOUSE_EXPRESS_REGION).success, response });
            callback && callback()
        } catch (e) {
            yield put({ type: apiType(EDIT_WAREHOUSE_EXPRESS_REGION).failure});
        }
    },
    * [apiType(GET_WAREHOUSE_ORDINARY_EXPRESS_LIST).request]  ({ params, callback }) {
        try {
            const response = yield getAxios('/inventory/web/warehouse/ordinary-express/list', params)
            yield put({ type: apiType(GET_WAREHOUSE_ORDINARY_EXPRESS_LIST).success, response });
            callback && callback()
        } catch (e) {
            yield put({ type: apiType(GET_WAREHOUSE_ORDINARY_EXPRESS_LIST).failure});
        }
    },
    * [apiType(GET_WAREHOUSE_ORDINARY_EXPRESS_REGION).request]  ({ params, callback }) {
        try {
            const response = yield getAxios('/inventory/web/warehouse/ordinary-express/region', params)
            yield put({ type: apiType(GET_WAREHOUSE_ORDINARY_EXPRESS_REGION).success, response });
            callback && callback()
        } catch (e) {
            yield put({ type: apiType(GET_WAREHOUSE_ORDINARY_EXPRESS_REGION).failure});
        }
    },
    * [apiType(EDIT_WAREHOUSE_ORDINARY_EXPRESS_REGION).request]  ({ params, callback }) {
        try {
            const response = yield postAxios('/inventory/web/warehouse/ordinary-express/region', params)
            yield put({ type: apiType(EDIT_WAREHOUSE_ORDINARY_EXPRESS_REGION).success, response });
            callback && callback()
        } catch (e) {
            yield put({ type: apiType(EDIT_WAREHOUSE_ORDINARY_EXPRESS_REGION).failure});
        }
    },
    * [apiType(GET_WAREHOUSE_SELECTION).request]  ({ params, callback }) {
        try {
            const response = yield getAxios('/inventory/common/selection/warehouse', params)
            yield put({ type: apiType(GET_WAREHOUSE_SELECTION).success, response });
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(GET_WAREHOUSE_SELECTION).failure});
        }
    },
    * [apiType(GET_ALL_WAREHOUSE_SELECTION).request]  ({ params, callback }) {
        try {
            const response = yield getAxios('/inventory/common/selection/warehouse?return_all=true', params)
            yield put({ type: apiType(GET_ALL_WAREHOUSE_SELECTION).success, response });
            callback && callback()
        } catch (e) {
            yield put({ type: apiType(GET_ALL_WAREHOUSE_SELECTION).failure});
        }
    },
    * [apiType(SET_SELECTED_WAREHOUSE).request]  ({ params, callback }) {
        try {
            yield put({ type: apiType(SET_SELECTED_WAREHOUSE).success, params });
        } catch (e) {
            yield put({ type: apiType(SET_SELECTED_WAREHOUSE).failure});
        }
    },
}
export default Object.keys(sagas).map(item => {
    return function * s() {
        yield takeEvery(item, function *(args) {
            try {
                yield sagas[item](args)
            } catch (e) {
                console.log(e)
            }
        })
    }
})